package com.ruiboyun.liveapp.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.ruiboyun.liveapp.R;
import com.ruiboyun.liveapp.base.BaseActivity;
import com.ruiboyun.liveapp.bean.Review;
import com.ruiboyun.liveapp.bean.Share;
import com.ruiboyun.liveapp.dialog.SharePopupWindow;
import com.ruiboyun.liveapp.util.tool.Loger;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WebActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.web)
    WebView web;
    private Review review;

    public static void go(Context context, String json) {
        Intent intent = new Intent(context, WebActivity.class);
        intent.putExtra("json", json);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_web;
    }

    @Override
    protected void afterCreate(@Nullable Bundle savedInstanceState) {
        String json = getIntent().getStringExtra("json");
        review = JSON.parseObject(json,Review.class);
        //设置可以缩放
        web.getSettings().setSupportZoom(true);
        //设置默认缩放尺寸FAR
//        web.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
        // 设置出现缩放工具
        web.getSettings().setBuiltInZoomControls(true);
        // 设置JavaScript可用
        web.getSettings().setJavaScriptEnabled(true);
        // 处理JavaScript对话框
//         处理各种通知和请求事件，如果不使用该句代码，将使用内置浏览器访问网页
        web.setWebChromeClient(new WebViewClient());
        web.setWebViewClient(new android.webkit.WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Loger.e("shouldOverrideUrlLoading");

                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                Loger.e("onPageFinished");
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                Loger.e(errorCode + "网页错误1" + description);

            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    Loger.e(error.getErrorCode() + "网页错误1" + error.getDescription());
                }

            }
        });

        web.getSettings().setUseWideViewPort(true);
        web.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        web.getSettings().setLoadWithOverviewMode(true);
        web.loadUrl(review.getPlay_addr());
    }

    class WebViewClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            Loger.e(newProgress + "progress");

        }
    }
    @OnClick({R.id.back,R.id.share})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                removeStack(this);
                break;
            case R.id.share:
                Share share=new Share();
                share.setTitle(review.getTitle());
                share.setContent(review.getDesc());
                share.setUrl(review.getPlay_addr());
                share.setLogo(review.getCover_addr());
                SharePopupWindow sharePopupWindow=new SharePopupWindow(this,share);
                sharePopupWindow.showAtLocation(view, Gravity.BOTTOM,0,0);
                break;
        }
    }
}
